/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frstat_system_count.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frstat_system.h"
#include "apmtest_cunit.h"

static int g_i_flag_run =1;
static unsigned long g_ul_pcap_queue_used=0L;

extern void frstat_system_count(void);

static unsigned int frame_threads_cnt(void)
{
    return 0;
}

static int frtaskskb_counts(void)
{
    return 0;
}

//conn_data 连接信息
typedef struct __conn_data_status {
    unsigned long ul_syn_conns;
    unsigned long ul_established_conns;
    unsigned long ul_disconnect_conns;
    unsigned long ul_not_active_conns;
    unsigned long ul_destroy_conns;
} conn_data_status;

typedef struct __conn_data_avg {
    unsigned long ul_syn_conns[4];
    unsigned long ul_established_conns[4];
    unsigned long ul_disconnect_conns[4];
} conn_data_avg;

typedef struct __conn_data {
    conn_data_status st_conn_data_total;
    conn_data_avg    st_conn_data_avg;
} conn_data;

//session_data session信息
typedef struct __session_data_status {
    unsigned long ul_login;
    unsigned long ul_logout;
} session_data_status;

typedef struct __session_data_avg {
    unsigned long ul_login[4];
    unsigned long ul_logout[4];
} session_data_avg;

typedef struct __session_data {
    session_data_status st_session_data_total;
    session_data_avg st_session_data_avg;
} session_data;

//总模块信息
typedef struct __module_stat {
    int  i_l7_proto;
    char *p_module_name;
    
    conn_data       st_conn_data;
    session_data    st_session_data;
} module_stat;

//总体的 包速率、流量、连接数
typedef struct __conn_stat {
    unsigned long ul_packets;   //数据包总数
    unsigned long ul_bytes;     //总流量
    unsigned long ul_conns;     //总连接数
    unsigned long ul_drops;     //总丢包数
} conn_stat;

typedef struct __conn_stat_avg {
    unsigned long ul_packets[4];//数据包总数
    unsigned long ul_bytes[4];  //总流量
    unsigned long ul_conns[4];  //总连接数
    unsigned long ul_drops[4];  //总丢包数
} conn_stat_avg;

//总体运行信息
typedef struct __system_stat {
    struct timeval start_time;  //程序开始运行时间
    unsigned int ui_memory;     //内存
    int i_pthreads;             //线程数
    
    conn_stat st_conn_total;    //总连接信息
    conn_stat_avg st_conn_avg;  //平均总连接信息

    Frhash_header st_module_stat;      //模块信息hash
    
    int last_time;              //毫秒
} system_stat;

extern system_stat g_st_system;

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

void case1(void)
{
    frstat_system_init(1);
    
    g_st_system.st_conn_total.ul_packets = 0;
    g_st_system.st_conn_total.ul_bytes = 0;
    g_st_system.st_conn_total.ul_conns = 0;
    g_st_system.st_conn_total.ul_drops = 0;
    
    memset(g_st_system.st_conn_avg.ul_packets, 0, sizeof(g_st_system.st_conn_avg.ul_packets));
    memset(g_st_system.st_conn_avg.ul_bytes, 0, sizeof(g_st_system.st_conn_avg.ul_bytes));
    memset(g_st_system.st_conn_avg.ul_conns, 0, sizeof(g_st_system.st_conn_avg.ul_conns));
    memset(g_st_system.st_conn_avg.ul_drops, 0, sizeof(g_st_system.st_conn_avg.ul_drops));
    
    frstat_system_module_conn_add(1433, 0, 0, 0, 0);
    frstat_system_module_inactive_add(1433, 0);
    frstat_system_module_session_add(1433, 0, 0);

    frstat_system_count();

    //frstat_system_output(NULL);
    
    frstat_system_destroy();

    return;
}

void case2(void)
{
    module_stat *p_module_node = NULL;
    Frlist_node* p_node = NULL;
    
    frstat_system_init(1);
    
    g_st_system.st_conn_total.ul_packets = 200;
    g_st_system.st_conn_total.ul_bytes = 2000;
    g_st_system.st_conn_total.ul_conns = 100;
    g_st_system.st_conn_total.ul_drops = 10;
    
    g_st_system.st_conn_avg.ul_packets[0] = 150;
    g_st_system.st_conn_avg.ul_bytes[0] = 1500;
    g_st_system.st_conn_avg.ul_conns[0] = 80;
    g_st_system.st_conn_avg.ul_drops[0] = 5;
    
    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1433;
    p_module_node->p_module_name = frstrdup("mysql");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 200;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 50;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 10;
    
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[0] = 150;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[0] = 80;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[0] = 50;
    
    p_module_node->st_session_data.st_session_data_total.ul_login = 10;
    p_module_node->st_session_data.st_session_data_total.ul_logout = 5;
    
    p_module_node->st_session_data.st_session_data_avg.ul_login[0] = 8;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[0] = 4;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1433, p_node);

    frstat_system_count();

    //frstat_system_output(NULL);
    
    frstat_system_destroy();

    return;

}

void case3(void)
{
    module_stat *p_module_node = NULL;
    Frlist_node* p_node = NULL;
    
    frstat_system_init(1);
    
    g_st_system.st_conn_total.ul_packets = 500;
    g_st_system.st_conn_total.ul_bytes = 10000;
    g_st_system.st_conn_total.ul_conns = 300;
    g_st_system.st_conn_total.ul_drops = 100;
    
    g_st_system.st_conn_avg.ul_packets[0] = 400;
    g_st_system.st_conn_avg.ul_bytes[0] = 8000;
    g_st_system.st_conn_avg.ul_conns[0] = 280;
    g_st_system.st_conn_avg.ul_drops[0] = 80;
    g_st_system.st_conn_avg.ul_packets[1] = 350;
    g_st_system.st_conn_avg.ul_bytes[1] = 7000;
    g_st_system.st_conn_avg.ul_conns[1] = 200;
    g_st_system.st_conn_avg.ul_drops[1] = 60;
    g_st_system.st_conn_avg.ul_packets[2] = 240;
    g_st_system.st_conn_avg.ul_bytes[2] = 5000;
    g_st_system.st_conn_avg.ul_conns[2] = 150;
    g_st_system.st_conn_avg.ul_drops[2] = 30;
    g_st_system.st_conn_avg.ul_packets[3] = 100;
    g_st_system.st_conn_avg.ul_bytes[3] = 2500;
    g_st_system.st_conn_avg.ul_conns[3] = 80;
    g_st_system.st_conn_avg.ul_drops[3] = 20;
    
    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1433;
    p_module_node->p_module_name = frstrdup("mysql");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 700;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 300;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 200;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 100;
    
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[0] = 550;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[0] = 200;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[0] = 150;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[1] = 450;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[1] = 150;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[1] = 100;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[2] = 300;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[2] = 100;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[2] = 80;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[3] = 200;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[3] = 60;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[3] = 60;
    
    p_module_node->st_session_data.st_session_data_total.ul_login = 10;
    p_module_node->st_session_data.st_session_data_total.ul_logout = 5;
    
    p_module_node->st_session_data.st_session_data_avg.ul_login[0] = 8;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[0] = 4;
    p_module_node->st_session_data.st_session_data_avg.ul_login[1] = 7;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[1] = 3;
    p_module_node->st_session_data.st_session_data_avg.ul_login[2] = 6;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[2] = 2;
    p_module_node->st_session_data.st_session_data_avg.ul_login[3] = 5;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[3] = 1;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1433, p_node);

    frstat_system_count();

    //frstat_system_output(NULL);
    
    frstat_system_destroy();

    return;

}

void case4(void)
{
    module_stat *p_module_node = NULL;
    Frlist_node* p_node = NULL;
    
    frstat_system_init(1);
    
    g_st_system.st_conn_total.ul_packets = 500;
    g_st_system.st_conn_total.ul_bytes = 10000;
    g_st_system.st_conn_total.ul_conns = 300;
    g_st_system.st_conn_total.ul_drops = 100;
    
    g_st_system.st_conn_avg.ul_packets[0] = 400;
    g_st_system.st_conn_avg.ul_bytes[0] = 8000;
    g_st_system.st_conn_avg.ul_conns[0] = 280;
    g_st_system.st_conn_avg.ul_drops[0] = 80;
    g_st_system.st_conn_avg.ul_packets[1] = 350;
    g_st_system.st_conn_avg.ul_bytes[1] = 7000;
    g_st_system.st_conn_avg.ul_conns[1] = 200;
    g_st_system.st_conn_avg.ul_drops[1] = 60;
    g_st_system.st_conn_avg.ul_packets[2] = 240;
    g_st_system.st_conn_avg.ul_bytes[2] = 5000;
    g_st_system.st_conn_avg.ul_conns[2] = 150;
    g_st_system.st_conn_avg.ul_drops[2] = 30;
    g_st_system.st_conn_avg.ul_packets[3] = 100;
    g_st_system.st_conn_avg.ul_bytes[3] = 2500;
    g_st_system.st_conn_avg.ul_conns[3] = 80;
    g_st_system.st_conn_avg.ul_drops[3] = 20;
    
    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1433;
    p_module_node->p_module_name = frstrdup("mysql");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 700;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 300;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 200;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 100;
    
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[0] = 550;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[0] = 200;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[0] = 150;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[1] = 450;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[1] = 150;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[1] = 100;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[2] = 300;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[2] = 100;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[2] = 80;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[3] = 200;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[3] = 60;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[3] = 60;
    
    p_module_node->st_session_data.st_session_data_total.ul_login = 10;
    p_module_node->st_session_data.st_session_data_total.ul_logout = 5;
    
    p_module_node->st_session_data.st_session_data_avg.ul_login[0] = 8;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[0] = 4;
    p_module_node->st_session_data.st_session_data_avg.ul_login[1] = 7;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[1] = 3;
    p_module_node->st_session_data.st_session_data_avg.ul_login[2] = 6;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[2] = 2;
    p_module_node->st_session_data.st_session_data_avg.ul_login[3] = 5;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[3] = 1;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1433, p_node);

    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1555;
    p_module_node->p_module_name = frstrdup("mysql");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 200;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 150;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 30;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 10;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 10;
    
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[0] = 150;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[0] = 120;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[0] = 20;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[1] = 100;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[1] = 70;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[1] = 12;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[2] = 50;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[2] = 30;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[2] = 7;
    p_module_node->st_conn_data.st_conn_data_avg.ul_syn_conns[3] = 0;
    p_module_node->st_conn_data.st_conn_data_avg.ul_established_conns[3] = 0;
    p_module_node->st_conn_data.st_conn_data_avg.ul_disconnect_conns[3] = 0;
    
    p_module_node->st_session_data.st_session_data_total.ul_login = 100;
    p_module_node->st_session_data.st_session_data_total.ul_logout = 50;
    
    p_module_node->st_session_data.st_session_data_avg.ul_login[0] = 80;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[0] = 40;
    p_module_node->st_session_data.st_session_data_avg.ul_login[1] = 70;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[1] = 30;
    p_module_node->st_session_data.st_session_data_avg.ul_login[2] = 60;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[2] = 20;
    p_module_node->st_session_data.st_session_data_avg.ul_login[3] = 50;
    p_module_node->st_session_data.st_session_data_avg.ul_logout[3] = 10;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1555, p_node);

    frstat_system_count();

    //frstat_system_output(NULL);
    
    frstat_system_destroy();

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("ut_frstat_system_count 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);
   	CU_add_test(pSuite, "case4", case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}